package com.yanfan.energybilling.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.energybilling.domain.PriceScheme;
import com.yanfan.energybilling.domain.PriceSchemeTime;
import com.yanfan.energybilling.mapper.PriceSchemeMapper;
import com.yanfan.energybilling.service.IPriceSchemeService;
import com.yanfan.energybilling.service.IPriceSchemeTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 价格方案Service业务层处理
 *
 * @author kerwincui
 * @date 2025-07-28
 */
@Service
public class PriceSchemeServiceImpl implements IPriceSchemeService {
    @Autowired
    private PriceSchemeMapper priceSchemeMapper;

    @Autowired
    private IPriceSchemeTimeService priceSchemeTimeService;

    /**
     * 查询价格方案
     *
     * @param id 价格方案主键
     * @return 价格方案
     */
    @Override
    public PriceScheme selectPriceSchemeById(Long id) {

        PriceScheme dbPriceScheme = priceSchemeMapper.selectPriceSchemeById(id);
        String type = dbPriceScheme.getChargeType();
        if (StringUtils.isEmpty(type)) {
            dbPriceScheme.setChargeTypeList(new ArrayList<>());
        } else {
            dbPriceScheme.setChargeTypeList(Arrays.asList(type.split(",")));
        }
        return dbPriceScheme;
    }

    /**
     * 查询价格方案列表
     *
     * @param priceScheme 价格方案
     * @return 价格方案
     */
    @Override
    public List<PriceScheme> selectPriceSchemeList(PriceScheme priceScheme) {
        return priceSchemeMapper.selectPriceSchemeList(priceScheme);
    }

    /**
     * 新增价格方案
     *
     * @param priceScheme 价格方案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPriceScheme(PriceScheme priceScheme) {
        priceScheme.setCreateTime(DateUtils.getNowDate());
        int i = priceSchemeMapper.insertPriceScheme(priceScheme);
        List<PriceSchemeTime> timeListNew = priceScheme.getTimeList();
        for (PriceSchemeTime time : timeListNew) {
            time.setSchemeId(priceScheme.getId());
        }
        // 批量保存
        priceSchemeTimeService.saveBatch(timeListNew);
        return i;
    }

    /**
     * 修改价格方案
     *
     * @param priceScheme 价格方案
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePriceScheme(PriceScheme priceScheme) {
        if (CollectionUtil.isNotEmpty(priceScheme.getChargeTypeList())) {
            priceScheme.setChargeType(String.join(",", priceScheme.getChargeTypeList()));
        }
        List<PriceSchemeTime> timeListNew = priceScheme.getTimeList();
        // 删除所有旧数据
        priceSchemeTimeService.deleteBySchemeId(priceScheme.getId());
        for (PriceSchemeTime time : timeListNew) {
            time.setSchemeId(priceScheme.getId());
        }
        // 批量保存
        priceSchemeTimeService.saveBatch(timeListNew);
        priceScheme.setUpdateTime(DateUtils.getNowDate());
        return priceSchemeMapper.updatePriceScheme(priceScheme);
    }

    /**
     * 批量删除价格方案
     *
     * @param ids 需要删除的价格方案主键
     * @return 结果
     */
    @Override
    public int deletePriceSchemeByIds(Long[] ids) {
        for (Long id : ids) {
            // 删除所有旧数据
            priceSchemeTimeService.deleteBySchemeId(id);
        }
        return priceSchemeMapper.deletePriceSchemeByIds(ids);
    }

    /**
     * 删除价格方案信息
     *
     * @param id 价格方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePriceSchemeById(Long id) {
        // 删除所有旧数据
        priceSchemeTimeService.deleteBySchemeId(id);
        return priceSchemeMapper.deletePriceSchemeById(id);
    }

    @Override
    public PriceScheme selectPriceSchemeByDeviceIdAnd(Long deviceId, Long thingsModelId) {
        return priceSchemeMapper.selectPriceSchemeByDeviceIdAnd(deviceId, thingsModelId);
    }
}
